package v.show.health.client;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;
import java.util.Map;

/**
 * @author whzhang
 */
@Slf4j
public class DefaultClientInfo implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Map<String, Object> map = new HashMap<>(10);
        // client info
        map.put("info.tags.environment",
                EnvHelper.getValue(environment, "${spring.profiles.active:}"));
        map.put("info.tags.owner",
                EnvHelper.getValue(environment, "${spring.cloud.nacos.discovery.metadata.owner:}"));
        map.put("info.tags.version",
                EnvHelper.getValue(environment, "${spring.cloud.nacos.discovery.metadata.version:}"));
        map.put("info.tags.namespace",
                EnvHelper.getValue(environment, "${spring.cloud.nacos.discovery.namespace:}"));
        map.put("info.tags.group",
                EnvHelper.getValue(environment, "${spring.cloud.nacos.discovery.group:}"));

        MapPropertySource source = new MapPropertySource("DefaultClientInfo", map);
        environment.getPropertySources().addLast(source);
    }
}
